package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import com.heima.wemedia.util.WmMediaThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;

/**
 * @Description TODO
 * @Author bo.li
 * @Date 2023/2/1 9:31
 * @Version 1.0
 */
@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private FileStorageService fileStorageService;

    @Override
    public ResponseResult upload(MultipartFile multipartFile) {
        //参数校验
        if(multipartFile == null || multipartFile.getSize() == 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //1.jpg   1.jpg  重新定义文件名，防止重复
        String oriFileName = multipartFile.getOriginalFilename();
        //获取文件后缀名
        String postfix = oriFileName.substring(oriFileName.lastIndexOf("."));
        String filePrefix = UUID.randomUUID().toString().replace("-","");
        //新文件名
        String newFileName = filePrefix + postfix;
        //上传
        String path = "";
        try {
            path = fileStorageService.uploadImgFile("",newFileName,multipartFile.getInputStream());
        } catch (IOException e) {
            log.error("文件上传失败",e);
            return ResponseResult.errorResult(AppHttpCodeEnum.WEMEDIA_UPLOAD_FAIL);
        }
        //存表
        WmMaterial material = new WmMaterial();
        material.setCreatedTime(new Date());
        material.setUserId(WmMediaThreadLocalUtil.getUser().getId());
        material.setUrl(path);
        material.setType(WmMaterial.TYPE_PIC);

        save(material);

        return ResponseResult.okResult(material);
    }

    @Override
    public ResponseResult list(WmMaterialDto dto) {
        //校验参数
        dto.checkParam();
        //分页查询
            IPage page = new Page(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<WmMaterial> wrapper = new LambdaQueryWrapper();
        //是否收藏
        if(dto.getIsCollection() != null && dto.getIsCollection() == 1){
            wrapper.eq(WmMaterial::getIsCollection,dto.getIsCollection());
        }
        //根据用户筛选
        wrapper.eq(WmMaterial::getUserId,WmMediaThreadLocalUtil.getUser().getId());
        //按创建时间降序
        wrapper.orderByDesc(WmMaterial::getCreatedTime);
        //查询
        this.page(page,wrapper);

        ResponseResult result = new PageResponseResult(dto.getPage(),dto.getSize(),(int)page.getTotal());
        result.setData(page.getRecords());

        return result;
    }
}